<?php
// +----------------------------------------------------------------------
// |管理员相关操作类
// +----------------------------------------------------------------------
// | Copyright (c) 2010 旋风工作室 All rights reserved.
// +----------------------------------------------------------------------
// | Author：XF.Zhoujz
// +----------------------------------------------------------------------

class AdminAction extends Action{
	//数据列表
	public function Index(){
		$this->validate();
		import("@.ORG.Page");  				//导入分页类
	    $Systems = D('Systems');  			//实例化model
	    $count = $Systems->count(); 		//查询管理员信息数
	    $p = new Page( $count, 10);         //实例化分页类   
	    $list=$Systems->findAll(); 
	    $p->setConfig('header','篇记录'); 
	    $p->setConfig('prev',"<"); 
	    $p->setConfig('next','>'); 
	    $p->setConfig('first','<<'); 
	    $p->setConfig('last','>>'); 
	    $page = $p->show(); 
	    $this->assign("nowpage",!empty($_GET["p"])?$_GET["p"]:1);
		$this->assign("page", $page); 
	    $this->assign("list", $list); 
	    $this->display('index'); 
	 }
 
// 编辑数据
	public function edit() {
		$this->validate();
		if(!empty($_GET['id'])) {
			$model = M('Systems');
			$vo	= $model->where("sys_id='".$_GET['id']."'")->find();
			if($vo) {
				$this->assign('vo',$vo);
				$this->display('edit');
			}else{
				$errmsg=$model->getError();
				$this->assign('errmsg',$errmsg);
				$this->display('edit');
			}
		}else{
			$errmsg='编辑项不存在！';
			$this->assign('errmsg',$errmsg);
			$this->display('edit');
		}
	}
	
	//更新数据
	public function update(){
		$this->validate();
		$model = M('Systems');
		if($_POST['sys_id']){
			$vo	= $model->where("sys_id='".$_POST['sys_id']."' and sys_password='".md5($_POST['password_old'])."'")->find();
			if($vo){
				$data['sys_password'] = md5($_POST['password_new']);
				$data['sys_type'] = $_POST['sys_type'];
				if(false !==$model->where("sys_id='".$_POST['sys_id']."'")->save($data)){
						$this->redirect('Admin/index');
				}else{
					$errmsg = $model->getError();
				}
			}else{
				$errmsg = '密码错误！请重新输入！';
			}
		}else{
			$errmsg = '管理员ID不能为空！';
		}
		$this->assign('errmsg',$errmsg);
		$this->assign('vo',$_POST);
		$this->display('edit');
	}
	
	//验证用户名
	public function checkAdminUser(){
		$this->validate();
		$systems = D('systems');
		$result = $systems->where("sys_id='".$_POST['sys_id']."'")->find();
		if($result){
			 $this->ajaxReturn(0,'账号已经存在！',0); 
		}else{
			 $this->ajaxReturn(1,'恭喜，账号可以使用！',1); 
		}
	}
	
//插入数据
	public function insert(){
		$this->validate();
		$model = D('systems');
		if(!$vo = $model->where("sys_id='".$_POST['sys_id']."'")->find()){
			if(trim($_POST['password'])===trim($_POST['password_conform'])){	
				$data['sys_id'] = trim($_POST['sys_id']);
				$data['sys_password'] = md5(trim($_POST['password']));
				$data['sys_type'] = trim($_POST['sys_type']);
				if($model->add($data)){
					$this->redirect('Admin/index');
				}else{
					$errmsg = $model->getDbError();
				}
			}
		}else{
			$errmsg = "账号已经存在，请更换账号后添加！";
		}		
		$this->assign('errmsg',$errmsg);
		$this->assign('vo',$_POST);
		$this->display('add');
	}
	
	//删除指定记录
	public function delete() {
		$this->validate();
		$model = D("systems");
		$id = $_REQUEST['id'];
		if (isset ($id)) {
			$condition = array ('sys_id' => array ('in', explode ( ',', $id ) ) );
			if (false !== $model->where ( $condition )->delete ()) {
				$this->redirect('Admin/index');
			} else {
				$errmsg = $model->getDbError();
			}
		}else{
			$errmsg = $model->getError();
		}
		$this->assign('errmsg',$errmsg);
		$this->display('index');
	}
	
	//登陆信息验证
	public function validate(){
		if(empty($_SESSION['admin'])){
			$this->redirect('Public/login');
		}
	}
}
?>